#!/bin/sh
if [ "$1" = "clean" ]; then
	# rm ./out/boot/*  ./out/kernel/*  *.img  log.bochsrc.disk
	find -type f -regex ".*\.o\|.*\.bin\|.*\.img\|.*log\.bochsrc\.disk\|.*\.img\.lock"  -delete	
	exit 0
fi


if [ ! -d "./out" ]; then
      mkdir out
fi
if [ ! -d "./out/boot" ];then 
    mkdir out/boot
fi 
if [ ! -d "./out/kernel" ];then 
    mkdir out/kernel 
fi

if [ ! -e "hd.img" ]; then
     /usr/bin/bximage -hd=1 -mode="create" -q hd.img
fi

nasm -I ./boot/include/ -o ./out/boot/mbr.bin ./boot/mbr.S && dd if=./out/boot/mbr.bin of=./hd.img bs=512 count=1  conv=notrunc
nasm -I ./boot/include/ -o ./out/boot/loader.bin ./boot/loader.S && dd if=./out/boot/loader.bin of=./hd.img bs=512 count=4 seek=2 conv=notrunc
nasm -f elf -o out/kernel/print.o lib/kernel/print.S
gcc -m32 -I lib/kernel/ -c -o out/kernel/main.o kernel/main.c
ld -melf_i386  -Ttext 0xc0001500 -e main -o ./out/kernel/kernel.bin out/kernel/main.o out/kernel/print.o && \
    dd if=./out/kernel/kernel.bin of=./hd.img bs=512 count=200 seek=9 conv=notrunc
